import Vue from "vue";
import Vuex from "vuex";
// import Vuex from "vuex";
import a from "./modules/a";
import b from "./modules/b";
Vue.use(Vuex);
const persitsPlugin = (store) => {
  let persists = localStorage.getItem('VUEX_PERSITS');
  if(persists){
    store.replaceState(JSON.parse(persists))
  }
  store.subscribe((mutation,state)=>{
 
    localStorage.setItem('VUEX_PERSITS',JSON.stringify(state))
  })
};
const store = new Vuex.Store({
  plugins: [persitsPlugin],
  strict: true,
  state: {
    // 珠峰年龄
    age: 12,
    a: 100,
    b: 200,
  },
  getters: {
    myAge(state) {
      return state.age + 20;
    },
  },
  mutations: {
    // commit找mutation
    addAge(state, payload) {
      state.age += payload;
    },
  },
  actions: {
    // dispatch找的是action
    addAge({ commit }, payload) {
      debugger;
      setTimeout(() => {
        commit("addAge", payload);
      }, 3000);
    },
  },
  modules: {
    a,
    b,
  },
});
// setTimeout(() => {
//   store.registerModule("e", {
//     state: {
//       a: 1,
//       b: 2,
//     },
//     getters:{
//       myA(state){
//         return state.a + 100;
//       }
//     }
//   });
// }, 1000);

export default store;
